{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Largest Positive Integer That Exists With Its Negative"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #two-pointers #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #双指针 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: findMaxK"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #与对应负数同时存在的最大正整数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个 <strong>不包含</strong> 任何零的整数数组 <code>nums</code> ，找出自身与对应的负数都在数组中存在的最大正整数 <code>k</code> 。</p>\n",
    "\n",
    "<p>返回正整数<em> </em><code>k</code> ，如果不存在这样的整数，返回 <code>-1</code> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [-1,2,-3,3]\n",
    "<strong>输出：</strong>3\n",
    "<strong>解释：</strong>3 是数组中唯一一个满足题目要求的 k 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [-1,10,6,7,-7,1]\n",
    "<strong>输出：</strong>7\n",
    "<strong>解释：</strong>数组中存在 1 和 7 对应的负数，7 的值更大。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [-10,8,6,7,-2,-3]\n",
    "<strong>输出：</strong>-1\n",
    "<strong>解释：</strong>不存在满足题目要求的 k ，返回 -1 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 1000</code></li>\n",
    "\t<li><code>-1000 &lt;= nums[i] &lt;= 1000</code></li>\n",
    "\t<li><code>nums[i] != 0</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [largest-positive-integer-that-exists-with-its-negative](https://leetcode.cn/problems/largest-positive-integer-that-exists-with-its-negative/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [largest-positive-integer-that-exists-with-its-negative](https://leetcode.cn/problems/largest-positive-integer-that-exists-with-its-negative/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[-1,2,-3,3]', '[-1,10,6,7,-7,1]', '[-10,8,6,7,-2,-3]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        nums = sorted(nums)\n",
    "        left = 0\n",
    "        right = len(nums) - 1\n",
    "        while left < right:\n",
    "            if nums[left] * nums[right] > 0:\n",
    "                return -1\n",
    "            if nums[left] + nums[right] == 0:\n",
    "                return nums[right]\n",
    "            elif nums[left] + nums[right] > 0:\n",
    "                right -= 1\n",
    "            else:\n",
    "                left += 1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution(object):\n",
    "    def findMaxK(self, nums):\n",
    "        nums.sort()\n",
    "        p , q = 0, len(nums) - 1\n",
    "        if nums[0] >= 0:\n",
    "            return -1\n",
    "        while p < q:\n",
    "            if abs(nums[p]) > nums[q]:\n",
    "                p += 1\n",
    "            elif abs(nums[p]) < nums[q]:\n",
    "                q -= 1\n",
    "            else:\n",
    "                return nums[q]\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        m=-1\n",
    "        for n in nums:\n",
    "            if -n in nums:\n",
    "                m=max(m,n)\n",
    "        return m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        k = -1\n",
    "        for x in nums:\n",
    "            if -x in nums:\n",
    "                k = max(k, x)\n",
    "        return k\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        mp = {}\n",
    "        maxNum = -1\n",
    "        for num in nums:\n",
    "            absNum = abs(num)\n",
    "            if absNum in mp and mp[absNum] + num == 0:\n",
    "                maxNum = max(maxNum, absNum)\n",
    "            mp[absNum] = num\n",
    "        return maxNum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        if min(nums) > 0:\n",
    "            return -1\n",
    "        save,ns = set(nums),sorted(nums,reverse=True)\n",
    "        for num in ns:\n",
    "            if 0-num in save:\n",
    "                return num\n",
    "            if num < 0:\n",
    "                return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        for i in range(len(nums)-1,-1,-1):\n",
    "            if -nums[i] in nums:\n",
    "                return nums[i]\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        dic = Counter(nums)\n",
    "        nums.sort(reverse=True)\n",
    "        for n in nums:\n",
    "            if -n in dic:\n",
    "                return n\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        nums = sorted(nums)\n",
    "        left = 0\n",
    "        right = len(nums) - 1\n",
    "        while left < right:\n",
    "            if nums[left] * nums[right] > 0:\n",
    "                return -1\n",
    "            if nums[left] + nums[right] == 0:\n",
    "                return nums[right]\n",
    "            if nums[left] + nums[right] > 0:\n",
    "                right -= 1\n",
    "                continue\n",
    "            if nums[left] + nums[right] < 0:\n",
    "                left += 1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        s = set(nums)\n",
    "        ans = -1\n",
    "        for x in s:\n",
    "           if -x in s:\n",
    "               ans = max(ans, x)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        k = -1\n",
    "        for x in nums:\n",
    "            if -x in nums:\n",
    "                k = max(k, x)\n",
    "        return k\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        nums = sorted(nums)\n",
    "        left = 0\n",
    "        right = len(nums) - 1\n",
    "        while left < right:\n",
    "            if nums[left] * nums[right] > 0:\n",
    "                return -1\n",
    "            if nums[left] + nums[right] == 0:\n",
    "                return nums[right]\n",
    "            elif nums[left] + nums[right] > 0:\n",
    "                right -= 1\n",
    "            else:\n",
    "                left += 1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        k = -1\n",
    "        for x in nums:\n",
    "            if -x in nums:\n",
    "                k = max(k, x)\n",
    "        return k\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        if nums[0]>0:\n",
    "            return -1\n",
    "        for i in nums:\n",
    "            if i < 0:\n",
    "                if i * -1 in nums:\n",
    "                    return i * -1\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        left, right = 0, len(nums) - 1\n",
    "        while left < right:\n",
    "            if nums[left] * nums[right] > 0:\n",
    "                return -1\n",
    "            if nums[left] + nums[right] == 0:\n",
    "                return nums[right]\n",
    "            elif nums[left] + nums[right] > 0:\n",
    "                right -= 1\n",
    "            else:\n",
    "                left += 1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        \"\"\"\n",
    "        nums.sort(reverse=True)\n",
    "        for i in nums:\n",
    "            if -i in nums:\n",
    "                return i\n",
    "                continue\n",
    "        return -1\n",
    "        \"\"\"\n",
    "        res = -1\n",
    "        s = set()\n",
    "        for i in nums:\n",
    "            if -i in nums:\n",
    "                res = max(res, abs(i))\n",
    "            s.add(i)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        k = -1\n",
    "        nums = set(nums)\n",
    "        for x in nums:\n",
    "            if -x in nums:\n",
    "                k = max(k, x)\n",
    "        return k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        ans=-1\n",
    "        for i in nums:\n",
    "            if -i in nums:\n",
    "                ans=max(ans,i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        nums.sort(reverse=True)\n",
    "        for i, item in enumerate(nums):\n",
    "            if item <= 0:\n",
    "                return -1\n",
    "            if -item in nums:\n",
    "                return item\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        k= -1\n",
    "        s= set(nums)\n",
    "        for num in nums:\n",
    "            if -num in s:\n",
    "                k= max(k, num)\n",
    "        return k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        s_num = set(nums)\n",
    "        ans = -1\n",
    "        for n in nums:\n",
    "            if n > 0 and -n in s_num:\n",
    "                ans = max(ans, n)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        k = -1\n",
    "        for x in nums:\n",
    "            if -x in nums:\n",
    "                k = max(k, x)\n",
    "        return k\n",
    "\n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        k = -1\n",
    "        for x in nums:\n",
    "            if -x in nums:\n",
    "                k = max(k,x)\n",
    "        return k\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        num = -1\n",
    "        min_nums = min(nums)\n",
    "        while min_nums < 0:\n",
    "            if len(nums) > 1:\n",
    "                if abs(min_nums) in nums:\n",
    "                    num = abs(min_nums)\n",
    "                    break\n",
    "\n",
    "                else:\n",
    "                    nums.remove(min_nums)\n",
    "                    min_nums = min(nums)\n",
    "            else:\n",
    "                break\n",
    "\n",
    "        return num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        target = -1\n",
    "        index = 1\n",
    "        for i in nums:\n",
    "            if (-i in nums[index:]) and (abs(-i) > target): \n",
    "                target = abs(-i)\n",
    "            index += 1\n",
    "        return target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        L = 0\n",
    "        R = len(nums) - 1\n",
    "        while L < R :\n",
    "            if abs(nums[L]) > abs(nums[R]) :\n",
    "                L += 1\n",
    "            elif abs(nums[L]) < abs(nums[R]) :\n",
    "                R -= 1\n",
    "            else :\n",
    "                if nums[L] == - nums[R] :\n",
    "                    return nums[R]\n",
    "                else :\n",
    "                    return -1\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        res = -1\n",
    "        hash_s = set()\n",
    "        nums.sort()\n",
    "        for num in nums:\n",
    "            if num < 0:\n",
    "                hash_s.add(num)\n",
    "            else:\n",
    "                if -num in hash_s:\n",
    "                    res = max(res,num)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        i = -1\n",
    "        for x in nums:\n",
    "            if -x in nums:\n",
    "                i = max(i, abs(x))\n",
    "        return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        for n in nums:\n",
    "            for m in nums:\n",
    "                if n + m == 0:\n",
    "                    return -n\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        h = nums[:]\n",
    "        ans = -1\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] * -1 in h:\n",
    "                ans = max(abs(nums[i]),ans)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        res = -1\n",
    "        hash_s = set(nums)\n",
    "        for num in nums:\n",
    "            if num > 0 and -num in hash_s:\n",
    "                res = max(res,num)\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        ans = -1\n",
    "        for x in nums:\n",
    "            if x * (-1) in nums:\n",
    "                ans = max(ans, abs(x))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        for num in sorted(nums, reverse=True):\n",
    "            if - num in nums:\n",
    "                return num\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        dic = Counter(nums)\n",
    "        nums.sort(reverse=True)\n",
    "        for n in nums:\n",
    "            if -n in dic:\n",
    "                return n\n",
    "            if n < 0:\n",
    "                break\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        for i in nums:\n",
    "            if i > 0 and (i*-1) in nums:\n",
    "                ans = max(ans,i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        n=sorted(nums)\n",
    "        l,r=0,len(n)-1\n",
    "        while l<r:\n",
    "            if n[l]>=0 or n[r]<=0:\n",
    "                return -1\n",
    "            nl,nr=abs(n[l]),abs(n[r])\n",
    "            if nl==nr:\n",
    "                return nr\n",
    "            elif nl<nr:\n",
    "                r-=1\n",
    "            else:\n",
    "                l+=1\n",
    "        return -1 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        max = -1\n",
    "        i = 0\n",
    "        while i < len(nums):\n",
    "            if -nums[i] in nums and nums[i]> max:\n",
    "                max = nums[i]\n",
    "            i +=1 \n",
    "        return max"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        nums_set = set(nums)\n",
    "        ans = []\n",
    "        for num in nums_set:\n",
    "            if -num in nums_set:\n",
    "                ans.append(abs(num))\n",
    "\n",
    "        return max(ans) if ans else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        k = -1\n",
    "        s = set(nums)\n",
    "        for x in s:\n",
    "            if -x in s:\n",
    "                k = max(k, x)\n",
    "        return k\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        s = set(nums)\n",
    "        return max((x for x in s if -x in s), default=-1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        v=0\n",
    "        for i in nums:\n",
    "            if -i in nums and i>0 and i>v:\n",
    "                v=i\n",
    "        if v==0:\n",
    "            return -1    \n",
    "        return v"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        k = -1\n",
    "        for x in nums:\n",
    "            if -x in nums:\n",
    "                k = max(k, x)\n",
    "        return k\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        nums_len = len(nums)\n",
    "        for i in range(nums_len - 1, 0, -1):\n",
    "            if i > 0 and nums[i] - nums[i] * 2 in nums:\n",
    "                return nums[i]\n",
    "\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        res = -1\n",
    "        hash_s = set()\n",
    "        nums.sort()\n",
    "        for num in nums:\n",
    "            if num < 0:\n",
    "                hash_s.add(num)\n",
    "            else:\n",
    "                if -num in hash_s:\n",
    "                    res = max(res,num)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        nums.sort(reverse=True)\n",
    "        for i in nums:\n",
    "            if -i in nums:\n",
    "                return i\n",
    "                continue\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        res = -1\n",
    "        hash_s = set()\n",
    "        nums.sort()\n",
    "        for num in nums:\n",
    "            if num < 0:\n",
    "                hash_s.add(num)\n",
    "            else:\n",
    "                if -num in hash_s:\n",
    "                    res = max(res,num)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        hashtable = {}\n",
    "        hashtable1 = {}\n",
    "        for i in nums:\n",
    "            if i < 0:\n",
    "                i = -i\n",
    "                hashtable[i] = 1\n",
    "            else:\n",
    "                hashtable1[i] = 1\n",
    "        result = []\n",
    "        for values in hashtable:\n",
    "            if values in hashtable1:\n",
    "                result.append(values)\n",
    "        if result == []:\n",
    "            return -1\n",
    "        return max(result) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        nums.sort(reverse=True)\n",
    "        for num in nums:\n",
    "            if -num in nums:\n",
    "                return num\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "   \n",
    "        for i in nums:\n",
    "            if i < 0 and (0 - i) in nums:\n",
    "                return 0 - i\n",
    "        else:\n",
    "            return -1\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        k = -1\n",
    "        s = set(nums)\n",
    "        for x in nums:\n",
    "            if -x in s:\n",
    "                k = max(k, x)\n",
    "        return k\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        nums = set(nums)\n",
    "        for i in range(1000,0,-1):\n",
    "            if i in nums and (-i) in nums:\n",
    "                return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        k = -1\n",
    "        for x in nums:\n",
    "            if -x in nums:\n",
    "                k = max(k, x)\n",
    "        return k\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        left,right=0,len(nums)-1\n",
    "        while left<=right:\n",
    "            if nums[left]+nums[right]==0:\n",
    "                return nums[right]\n",
    "            elif abs(nums[left]) < nums[right]:\n",
    "                right -=1\n",
    "            elif abs(nums[left]) > nums[right]:\n",
    "                left +=1\n",
    "            else:\n",
    "                right -=1\n",
    "                left +=1\n",
    "        return -1\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        return max([x for x in nums if x in nums and -x in nums], default=-1)\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        ar = []\n",
    "        for x in nums:\n",
    "            ne = -x\n",
    "            if ne in nums:\n",
    "                ar += [x]\n",
    "        return max(ar) if ar else -1            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        res = -1\n",
    "        hash_s = set(nums)\n",
    "        for num in nums:\n",
    "            if num > 0 and -num in hash_s:\n",
    "                res = max(res,num)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        k = -1\n",
    "        s = set(nums)\n",
    "        for x in nums:\n",
    "            if -x in s:\n",
    "                k = max(k, x)\n",
    "        return k\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        h = []\n",
    "        ans = -1\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] * -1 in h:\n",
    "                if abs(nums[i]) > ans:\n",
    "                    ans = abs(nums[i])\n",
    "            h.append(nums[i])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        hax = set()\n",
    "        res = -1\n",
    "        for x in sorted(nums):\n",
    "            if x < 0:\n",
    "                hax.add(x)\n",
    "            else:\n",
    "                if (-x) in hax:\n",
    "                    res = max(res, x)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "\n",
    "        nums.sort(reverse=True)\n",
    "        # print(nums)\n",
    "        res=[]\n",
    "        left=0\n",
    "        right=len(nums)-1\n",
    "        while left<right:\n",
    "            if nums[left]==(-nums[right]):\n",
    "                # print(f'相等的时候,左指针 ',nums[left])\n",
    "                res.append(nums[left])\n",
    "                left+=1\n",
    "                right-=1\n",
    "            elif nums[left]<(-nums[right]):\n",
    "                # print('-right',-nums[right])\n",
    "                right-=1\n",
    "            else:\n",
    "                left+=1\n",
    "        # print(res)\n",
    "        return max(res) if len(res)>=1 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        s =set()\n",
    "        ans = -1\n",
    "        for n in nums:\n",
    "            if -n in s:\n",
    "                ans = max(abs(n),ans)\n",
    "            else:\n",
    "                s.add(n)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "\n",
    "        nums.sort()\n",
    "        numss = nums[::-1]\n",
    "\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(i+1, len(nums)):\n",
    "                if (-1)*numss[i] == numss[j]:\n",
    "                    return numss[i]\n",
    "\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        hax = set()\n",
    "        nums.sort()\n",
    "        for x in nums:\n",
    "            if x < 0:\n",
    "                hax.add(x)\n",
    "        for y in nums[::-1]:\n",
    "            if (-y) in hax:\n",
    "                return y\n",
    "        return -1\n",
    "\n",
    "                    \n",
    "            \n",
    "                \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        b=len(nums)\n",
    "        while b:\n",
    "            b-=1\n",
    "            a=max(nums)\n",
    "            if -a in nums:\n",
    "                return a\n",
    "            else:\n",
    "                nums.remove(a)\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        a,b=0,[]\n",
    "        for i in nums:\n",
    "            if -i in nums:\n",
    "                b.append(i)\n",
    "        return max(b) if b else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        # s = set()\n",
    "        # ans = -1\n",
    "        # for x in nums:\n",
    "        #     if -x in s:\n",
    "        #         ans = max(ans,abs(x))\n",
    "        #     s.add(x)\n",
    "        # return ans \n",
    "\n",
    "        # s = set()\n",
    "        # ans =-1 \n",
    "        # for x in nums:\n",
    "        #     if -x in s:\n",
    "        #         ans = max(ans,abs(x))\n",
    "        #     s.add(x)\n",
    "        # return ans \n",
    "\n",
    "        s= set()\n",
    "        ans = -1\n",
    "        for x in nums:\n",
    "            if -x in s :\n",
    "                ans = max(ans,abs(x))\n",
    "            s.add(x)\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        s=[]\n",
    "        for i in nums:\n",
    "            for b in nums:\n",
    "                if i==b*-1 :\n",
    "                    s.append(i)\n",
    "        if len(s)!=0:\n",
    "            max1=s[0]\n",
    "            for c in range(len(s)):\n",
    "                if max1<s[c]:\n",
    "                    max1=s[c]\n",
    "       \n",
    "            return (max1)\n",
    "        else:\n",
    "            return(-1)\n",
    "\n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        if nums[0]>=0:\n",
    "            return -1\n",
    "        less_zero = [i for i in nums if i < 0]\n",
    "        for i in less_zero:\n",
    "            if i* -1 in nums:\n",
    "                return i* -1\n",
    "            else:\n",
    "                continue\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        a = {-i for i in nums if i < 0}\n",
    "        b = {i for i in nums if i > 0}\n",
    "        return -1 if not a & b else max(a & b)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findMaxK(self, nums: List[int]) -> int:\n",
    "        count_max = -1\n",
    "        for x in nums:\n",
    "            if -x in nums:\n",
    "                count_max = max(count_max, x)\n",
    "        return count_max"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
